package action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import model.AlergyDetail;
import model.User;
import org.apache.struts2.interceptor.ParameterAware;
import service.UserServiceImpl;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: TM
 * Date: 11/10/12
 * Time: 11:22 PM
 * To change this template use FileModel | Settings | FileModel Templates.
 */
public class PersonalDetailAction  extends ActionSupport implements ModelDriven ,ParameterAware {

    User user = new User();
    private UserServiceImpl userService;
    private Map parameters;

    private List<AlergyDetail> alergyDetailsList;

    public String setInformation()
	{     ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        user = (User) session.get("USER");
        System.out.println(user.getFname() + user.getLname());
		return "information";
	}

    public String setAleargy()
	{     ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        user = (User) session.get("USER");
        System.out.println(user.getFname() + user.getLname());
        alergyDetailsList = userService.getAlergyDetails(user);
		return "alergy";
	}
    public String saveAleargy()
	{     ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        user = (User) session.get("USER");
        System.out.println(user.getFname() + user.getLname());
       if(userService.saveAlergyDetails(user, new AlergyDetail(((String[])parameters.get("drugName"))[0], ((String[])parameters.get("description"))[0])) > 0){
         alergyDetailsList = userService.getAlergyDetails(user);
         return "alergy";
       }else {
           return "error";
       }

	}

        public String askQuestion()
	{     ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        user = (User) session.get("USER");
        System.out.println(user.getFname() + user.getLname());
		return "askQuestion";
	}

    public String updateUserInformation(){

        System.out.println("updateUserInformation");
        System.out.println(user.getLname());

        if(userService.updateUserInformation(user) >0){
        return "success";
        } else {
            return "error";
        }
    }

    public Object getModel() {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        user = (User) session.get("USER");
        return user;  //To change body of implemented methods use FileModel | Settings | FileModel Templates.
    }

    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }

    public UserServiceImpl getUserService() {
        return userService;
    }

    public List<AlergyDetail> getAlergyDetailsList() {
        return alergyDetailsList;
    }

    public void setAlergyDetailsList(List<AlergyDetail> alergyDetailsList) {
        this.alergyDetailsList = alergyDetailsList;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Map getParameters() {
        return parameters;
    }

    public void setParameters(Map parameters) {
        this.parameters = parameters;
    }
}
